/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.shop.device.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.opsli.api.wrapper.shop.userorder.SysUserOrderModel;
import org.opsli.common.utils.WrapperUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.opsli.core.base.service.impl.CrudServiceImpl;

import org.opsli.modulars.shop.device.entity.SysShopDevice;
import org.opsli.api.wrapper.shop.device.SysShopDeviceModel;
import org.opsli.modulars.shop.device.service.ISysShopDeviceService;
import org.opsli.modulars.shop.device.mapper.SysShopDeviceMapper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j


/**
 * 门店设备 Service Impl
 *
 * @author 17607080935
 * @date 2025-01-08 17:12:50
 */
@Service
public class SysShopDeviceServiceImpl extends CrudServiceImpl<SysShopDeviceMapper, SysShopDevice, SysShopDeviceModel>
    implements ISysShopDeviceService {

    @Autowired(required = false)
    private SysShopDeviceMapper mapper;


    @Override
    public List<SysShopDeviceModel> findPageWeChat(String orgIds, Date date, Integer pageNo, Integer pageSize) {

        QueryWrapper q = new QueryWrapper<>();
        q.like("org_ids", orgIds);
        Page<SysShopDevice> page = new Page<>(pageNo, pageSize);
        // 执行分页查询
        IPage<SysShopDevice> result = mapper.selectPage(page, q);

        List<SysShopDeviceModel> list = WrapperUtil.transformInstance(result.getRecords(), SysShopDeviceModel.class);


        return list;
    }

    /**
     * 组装门店设备当天的订单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysShopDeviceModel assembleShopDeviceOrder( SysShopDeviceModel o, List<SysUserOrderModel> list) {


        List<Integer> timeNodes = new ArrayList<>();
        Integer currHour = DateUtil.hour(new Date(),true);
        for (SysUserOrderModel sysUserOrderModel : list) {
            //组装时间节点

            sysUserOrderModel.getStartTime();
            sysUserOrderModel.getEndTime();

            int StartTimeHour = DateUtil.hour(sysUserOrderModel.getStartTime(),true);
            int StartTimeMinute = DateUtil.minute(sysUserOrderModel.getStartTime());
            int EndTimeHour = DateUtil.hour(sysUserOrderModel.getEndTime(),true);
            int EndTimeMinute = DateUtil.minute(sysUserOrderModel.getEndTime());
            log.info("开始时间：{}，结束时间：{}",StartTimeHour,EndTimeHour);
//            log.info("开始时间：{}，结束时间：{}",StartTimeMinute,EndTimeMinute);

//            bug当结束时间小时小于当前小时 时会出问题
            //如果结束时间分钟小于3，则结束时间小时减1
            if (EndTimeMinute<=1){
                EndTimeHour = EndTimeHour - 1;
            }
            for (int i = StartTimeHour; i <= StartTimeHour+sysUserOrderModel.getHourNumber(); i++) {
                int timeNode = i;

                if (i>=24){
                    timeNode = i%24;
                }
                //如果开始时间小时大于当前时间小时，则开始时间小时等于当前时间小时
                if (DateUtil.compare(sysUserOrderModel.getStartTime(), new Date())<0&&DateUtil.compare(sysUserOrderModel.getEndTime(), new Date())>0){
                    if (i>=currHour){
                        timeNodes.add(timeNode);
                    }
                }else {
                    timeNodes.add(timeNode);
                }

            }

        }
        o.setTimeNode(timeNodes);
        return o;
    }
}